﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.PresentationModel;
using SharedGenomics.Workbench.Services;
using SharedGenomics.Domain;

namespace SharedGenomics.Workbench.Presenters
{
    public class AddSchemaPresenter : Presenter
    {
        private FileSchema _schema;

        public AddSchemaPresenter(IAddSchemaView view, IFileBrowserPresenter openFile, IFileSchemaRepository fsRep)
            : base(view)
        {
            this._schema = new SharedGenomics.Domain.FileSchema();

            openFile.SetFilters("Xml Files | *.xml", "All Files | *.*");
            openFile.FileOpened += new EventHandler(this.OpenFile_FileOpened);

            view.SelectedCategoricalColumnChanged += new EventHandler(this.View_SelectedCategoricalColumnChanged);
            view.SelectedContinuousColumnChanged += new EventHandler(this.View_SelectedContinuousColumnChanged);
            view.AddClicked += new EventHandler(this.View_AddClicked);
            view.CancelClicked += new EventHandler(this.View_CancelClicked);

            view.IsAddEnabled = false;

            this.SchemaView = view;
            this.OpenFile = openFile;
            this.SchemaRepository = fsRep;
        }

        private void View_SelectedContinuousColumnChanged(object sender, EventArgs e)
        {
            if(this.SchemaView.SelectedContinuousColumn != null)
            {
                //clear any selected categorical columns in the view
                this.SchemaView.ClearSelectedCategorialColumns();
                this.SchemaView.DisplayedColumn = this.SchemaView.SelectedContinuousColumn;
            }
        }

        private void View_SelectedCategoricalColumnChanged(object sender, EventArgs e)
        {
            if (this.SchemaView.SelectedCategorialColumn != null)
            {
                this.SchemaView.ClearSelectedContinuousColumns();
                this.SchemaView.DisplayedColumn = this.SchemaView.SelectedCategorialColumn;
            }
        }

        private void OpenFile_FileOpened(object sender, EventArgs e)
        {
            //try to load model
            this._schema = FileSchema.LoadFromXml(this.OpenFile.FilePath);
            this.SchemaView.IsAddEnabled = true;
            this.SchemaView.ContinuousColumns = this._schema.ContinuousColumns.Select(c => new FileSchemaColumnDTO(c));
            this.SchemaView.CategorialColumns = this._schema.CategoricalColumns.Concat(this._schema.BinaryColumns).Select(c => new FileSchemaColumnDTO(c));
        }

        private void View_AddClicked(object sender, EventArgs e)
        {
            string trimmedName = this.SchemaView.SchemaName.Trim();
            if (trimmedName.Length > 0)
            {
                this._schema.Name = trimmedName;
                this.SchemaRepository.AddSchema(this._schema);
                this.SchemaAdded = true;
                this.RequestClose();
            }
            else
            {
                this.SchemaView.DisplayError("Please enter a name for the schema");
            }
        }

        private void View_CancelClicked(object sender, EventArgs e)
        {
            this.RequestClose();
        }

        public bool SchemaAdded { get; private set; }
        private IAddSchemaView SchemaView { get; set; }
        private IFileBrowserPresenter OpenFile { get; set; }
        private IFileSchemaRepository SchemaRepository { get; set; }

        [Region("OpenFile")]
        public IPresenter OpenFilePresenter
        {
            get { return this.OpenFile; }
        }
    }
}
